import pandas as pd
pd.set_option('display.max_rows', None)
pd.set_option('display.max_columns', None)
pd.set_option('expand_frame_repr',False)
# pd.set_option('mode.chained_assignment', None)

def traj_segment(data):
    # data = pd.read_csv(path)
    data["traj_segment"] = 0
    shiplist=list()
    MMSIDATA=data['mmsi']
    SHIPSMMSI = list(set(MMSIDATA)) #这个csv文件所包含的mmsi
    shipcount=len(SHIPSMMSI) #这个scv文件包含多少条船
    for i in range(shipcount):
        mmsi = SHIPSMMSI[i]
        # ONESHIP = data.iloc[:, :][data[data.T.index[0]] == mmsi]
        ONESHIP = data.loc[data.mmsi == mmsi]
        ONESHIPBEGIN = ONESHIP.index[0]
        ONESHIPEND = ONESHIP.index[-1]
        flag = 0
        point_num = len(ONESHIP)
        for j in range(ONESHIPBEGIN,ONESHIPEND+1):
            if point_num < 2:
                break
            if j == ONESHIPEND:
                # data["traj_segment"].loc[j] = flag
                data.loc[data.index == j,'traj_segment'] = flag
                break
            # data["traj_segment"].loc[j] = flag
            data.loc[data.index == j, 'traj_segment'] = flag
            time = (data.iloc[j + 1])['Unixtime'] - (data.iloc[j])['Unixtime']
            if time > 600:
                flag = flag+1
    return data